using System;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OpenWaves.ImageTransformations.Web.Controls
{
    [ParseChildren(true)]
    public class ResponsiveBackgroundImagePanel : Control
    {
        public string ImageUrl { get; set; }

        public ImageTransformationType Transformation { get; set; }

        public string CustomTransformation { get; set; }

        public string CssClass { get; set; }

        private readonly ImageBreakpointCollection breakpoints = new ImageBreakpointCollection();
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public ImageBreakpointCollection Breakpoints
        {
            get { return this.breakpoints; }
        }

        private readonly PlaceHolder content = new PlaceHolder();
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public PlaceHolder Content
        {
            get { return this.content; }
        }

        private bool? renderXhtmlCompilantMarkup;
        public bool RenderXhtmlCompilantMarkup
        {
            get { return this.renderXhtmlCompilantMarkup ?? ResponsiveImage.Settings.RenderXhtmlCompilantMarkup; }
            set { this.renderXhtmlCompilantMarkup = value; }
        }

        protected override void OnInit(EventArgs e)
        {
            this.Controls.Add(this.Content);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (string.IsNullOrEmpty(this.ImageUrl))
                return;

            if (this.RenderXhtmlCompilantMarkup)
            {
                var b = ResponsiveImage.GetResponsiveImageBreakpoints(this, this.ResolveUrl(this.ImageUrl), this.Breakpoints, this.Transformation,
                                                         this.CustomTransformation);
                var script = new StringBuilder();
                foreach (var br in b)
                {
                    script.AppendFormat("$('#{0}').data('{1}', '{2}');", this.ClientID, br.Key, br.Value);
                }
                this.Page.ClientScript.RegisterStartupScript(typeof(ResponsiveImage), this.UniqueID, script.ToString(), true);
            }
        }

        protected virtual string ResolveImageUrl(string url)
        {
            return this.ResolveUrl(url);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (this.RenderXhtmlCompilantMarkup)
                writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);

            if (string.IsNullOrEmpty(this.CssClass) == false)
                writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass);

            if (this.RenderXhtmlCompilantMarkup == false)
            {
                var data = ResponsiveImage.GetResponsiveImageBreakpoints(this, this.ResolveUrl(this.ImageUrl), this.Breakpoints,
                                                                         this.Transformation,
                                                                         this.CustomTransformation);
                foreach (var dataPair in data)
                {
                    writer.AddAttribute("data-" + dataPair.Key, dataPair.Value);
                }
            }


            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            base.Render(writer);
            writer.RenderEndTag();
        }
    }
}